package com.example.controller.statistics;

import com.example.pojo.Product;
import com.example.service.statistics.ProductHitsService;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/*
 *  @ClassName:ProductHitsController
 *  @Author:WZ
 *  @DateTime:2023/1/31/10:59
 *  商品统计---商品的点赞热度
 */
@CrossOrigin
@RestController
@RequestMapping("/ProductHits")
public class ProductHitsController {
    private static List<Product> daoChu;

    @Autowired
    private ProductHitsService productHitsService;

    //7天
    @RequestMapping("/getProductHitsZj7t")
    public List<Product> getProductHitsZj7t(){
        List<Product> list7t = productHitsService.getProductHitsZj7t();
        return list7t;
    }

    //30天
    @RequestMapping("/getProductHitsZj30t")
    public List<Product> getProductHitsZj30t(){
        List<Product> list30t =productHitsService.getProductHitsZj30t();
        return list30t;
    }

    //90天
    @RequestMapping("getProductHitsZj90t")
    public List<Product> getProductHitsZj90t(){
        List<Product> list90t =productHitsService.getProductHitsZj90t();
        return list90t;
    }

    //全部
    @RequestMapping("/getProductHits")
    public List<Product> getProductHits(){
        List<Product> list  = productHitsService.getProductHits();
        return list;
    }

    //日期模糊查询
    @RequestMapping("/getProductHitsLike")
    public List getProductHitsLike(@RequestParam("kaiShiLike")String s1, @RequestParam("jieShuLike")String s2){
        List<Product> productHitsLike = productHitsService.getProductHitsLike(s1,s2);
        daoChu = productHitsLike;
        return productHitsLike;
    }

    //导出数据
    @PostMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("sheet1");
        String fileName = "商品点赞热度.xls";
        List<Product> list = daoChu;
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("商品ID");
        row.createCell(1).setCellValue("商品名称");
        row.createCell(2).setCellValue("点击量");
        row.createCell(3).setCellValue("商品简介");
        row.createCell(4).setCellValue("商品现在价格");
        row.createCell(5).setCellValue("商品之前价格");

        Product entity = null;

        for (int i = 0; i < list.size(); i++) {
            entity = list.get(i);

            row = sheet.createRow(i + 1); // 从第2行开始填充数据
            // 序号
            row.createCell(0).setCellValue(String.valueOf(i + 1));
            if (entity.getProductName() != null) {
                row.createCell(1, CellType.STRING).setCellValue(entity.getProductName());
            } else {
                row.createCell(1, CellType.STRING).setCellValue("null");
            }

            if (entity.getHits() != null) {
                row.createCell(2, CellType.STRING).setCellValue(entity.getHits());
            } else {
                row.createCell(2, CellType.STRING).setCellValue("null");
            }

            if (entity.getProductDescription() != null) {
                row.createCell(3).setCellValue(entity.getProductDescription());
            } else {
                row.createCell(3).setCellValue("null");
            }
            if (entity.getCurrentpriceProduct() != 0.00) {
                row.createCell(4).setCellValue(entity.getCurrentpriceProduct());
            } else {
                row.createCell(4).setCellValue("0.00");
            }

            if (entity.getOriginalpriceProduct() != 0.00) {
                row.createCell(5).setCellValue(entity.getOriginalpriceProduct());
            } else {
                row.createCell(5).setCellValue("0.00");
            }

        }
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition",
                "attachment;filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
        response.flushBuffer();

        workbook.write(response.getOutputStream());
    }
}
